<template>
	<view class="charts-box">
		<u-navbar :title="title" :autoBack="true" :placeholder="true">
		</u-navbar>
		<qiun-data-charts type="line" :opts="opts" :chartData="chartData" />
	</view>
</template>

<script>
	import {
		setData
	} from '@/util.js'
	let app = getApp();
	export default {
		onLoad(option) {
			if(option.type == 'purchaseAmount') this.title = '采购总额'
			if(option.type == 'purchasePaidAmount') this.title = '采购已付金额'
			if(option.type == 'storeNums') this.title = '门店数量'
			if(option.type == 'materialGrossProfit') this.title = '材料毛利'
			if(option.type=='saleAmount') this.title = '销售金额'
		},
		data() {
			return {
				title:'数据可视化',
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: [app.globalData.defaultColor],
					fontSize: 12,
					padding: [15, 10, 0, 0],
					dataLabel: false,
					dataPointShape: false,
					legend: {},
					xAxis: {
						labelCount: 5, //默认显示个数
						rotateLabel: true,
						fontColor: app.globalData.defaultColor,
						marginTop:10,
						// disabled:true,
						// axisLine:false
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2,
						data: [{
							min: 0,
						}]
					},
					extra: {
						line: {
							type: "curve",
							width: 2,
							activeType: "hollow",
							linearType: "custom",
							onShadow: true,
							animation: "horizontal"
						}
					}
				}
			};
		},
		onReady() {
			this.getServerData();
		},
		methods: {
			getServerData() {
				const dateArr = [{
						date: '2024-01-01',
						count: 0
					},
					{
						date: '2024-03-21',
						count: 1800
					},
					{
						date: '2024-04-21',
						count: 200.32
					},
					{
						date: '2024-05-01',
						count: 100
					},
					{
						date: '2024-05-17',
						count: 1300
					},
				];
				let filterData = setData(dateArr);

				let data = []
				let date = []
				filterData.map(el => {
					date.push(el.date);
					data.push(el.count)
				})

				//模拟从服务器获取数据时的延时
				setTimeout(() => {
					//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					let res = {
						categories: date,
						series: [

							{
								name: "采购数据",
								data: data
							},

						]
					};
					this.chartData = JSON.parse(JSON.stringify(res));
					this.opts.xAxis.itemCount = data.length
					console.log(this.opts.xAxis)
				}, 100);
			},
		}
	};
</script>

<style scoped>
	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		width: 100%;
		height: 300px;
	}
</style>